package com.yjz.jvm.juc.ExecutorService.task;

import com.yjz.jvm.juc.ExecutorService.result.CalculationTaskResult;
import com.yjz.jvm.juc.ExecutorService.vo.Student;
import lombok.extern.slf4j.Slf4j;

import java.io.File;
import java.io.FileOutputStream;
import java.util.List;
import java.util.concurrent.Callable;

/**
 * @author :yuanJinZhong
 * @description: 计算任务
 * @date :2019/1/3-23:10
 */
@Slf4j
public class CalculationTask implements Callable<CalculationTaskResult> {

  private List<Student> studentList;

  //引入了共享变量，结果不可控制,否决
  //private CalculationTaskResult result;

  public CalculationTask(List<Student> studentList/*, CalculationTaskResult result*/) {
    this.studentList = studentList;
    /* this.result = result;*/
  }

  /**
   * 计算传入的学生列表的分数总和
   *
   * @return 计算结果
   * @throws Exception
   */
  @Override
  public CalculationTaskResult call() throws Exception {
    //每个任务有自己的结果,主线程汇总
    CalculationTaskResult result = new CalculationTaskResult();
    int totalScore = 0;
    for (Student student : studentList) {
      totalScore += student.getScore();
    }
    result.setTotalScore(totalScore);
    //模拟耗时任务
    Thread.sleep(10000);
    log.warn("任务的执行结果是: {} ", totalScore);
    return result;
  }
}
